package d4_demo;

/**
 * 2024/6/13 Cjj 啤酒案例 9：11
 */
public class Test1 {
    public static int totalNumber;//总酒数
    public static int lastBottleNumber;
    public static int lasCoverNumber;

    public static void main(String[] args) {
        //啤酒2元一瓶,4个盖子可以换一瓶,2个空瓶可以换一瓶,请问10元可以喝多少瓶?
        buy(10);

        System.out.println("总数：" + totalNumber);
        System.out.println("剩余瓶子数：" + lastBottleNumber);
        System.out.println("剩余盖子数：" + lasCoverNumber);
    }

    public static void buy(int money) {
        //1.先买
        int buyNumber = money / 2;
        totalNumber += buyNumber;

        //2.把盖子喝瓶子换算成钱继续买
        //计算本轮总的盖子和瓶子数
        /**瓶子**/ int allBottleNumber = buyNumber + lastBottleNumber;
        /**盖子**/ int allCoverNumber =  buyNumber + lasCoverNumber;

        int allmoney = 0;

        if (allBottleNumber >= 2) {
            allmoney += (allBottleNumber / 2) * 2;
        }
        lastBottleNumber = allBottleNumber % 2;

        if (allCoverNumber >= 4) {
            allmoney += (allCoverNumber / 4) * 2;
        }
        lasCoverNumber = allCoverNumber % 4;

        if (allmoney >= 2) {
            buy(allmoney);
        }
    }
}
